/*
 * @copyright 2011 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of GameTimer.
 *
 * GameTimer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GameTimer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GameTimer. If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiption.gametimer.view;

import org.amphiption.gametimer.R;
import org.amphiption.gametimer.dialog.ColorPickerDialog;
import org.amphiption.gametimer.entity.Player;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * @author Amphiprion
 * 
 */
public class PlayerView extends LinearLayout {
	/** the linked player. */
	private Player player;
	/** The play stat clicked listener. */
	private OnPlayerClickedListener playerClickedListener;

	/** The imageview. */
	private ImageView img;

	private LinearLayout playerLayout;
	private Button bt;

	private boolean handicap;
	private TextView tvName;
	private TextView tvDuration;

	/**
	 * Construct a player view.
	 * 
	 * @param context
	 *            the context
	 * @param playStat
	 *            the player entity
	 */
	public PlayerView(Context context, Player player, OnPlayerClickedListener playerClickedListener, boolean handicap) {
		super(context);
		this.player = player;
		this.playerClickedListener = playerClickedListener;
		this.handicap = handicap;
		LayoutParams lp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		setLayoutParams(lp);
		// setBackgroundDrawable(context.getResources().getDrawable(R.drawable.list_item_background_states));
		setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (PlayerView.this.player == null) {
					PlayerView.this.playerClickedListener.playerClicked(PlayerView.this);
				}
			}
		});
		addView(createIcon());

		addView(createCategoryLayout());

		addView(createEditButton());

		fillData();
	}

	/**
	 * @return the player
	 */
	public Player getPlayer() {
		return player;
	}

	/**
	 * Create the category icon view.
	 * 
	 * @return the view
	 */
	private View createIcon() {
		ImageView img = new ImageView(getContext());
		LayoutParams imglp = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		imglp.gravity = Gravity.CENTER_VERTICAL;
		imglp.rightMargin = 5;
		img.setLayoutParams(imglp);

		if (player == null) {
			img.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.add));
		} else {
			img.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.remove));
		}
		img.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				playerClickedListener.playerClicked(PlayerView.this);
			}
		});
		this.img = img;
		return img;
	}

	/**
	 * Create the category layout view
	 * 
	 * @return the view
	 */
	private View createCategoryLayout() {
		playerLayout = new LinearLayout(getContext());
		LayoutParams aclp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 3);
		playerLayout.setOrientation(VERTICAL);
		playerLayout.setLayoutParams(aclp);
		// LayoutParams tlp = new
		// LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
		// android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		// txtPlayerName.setLayoutParams(tlp);

		tvName = new TextView(getContext());
		tvName.setTextSize(22);
		tvName.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				editStat(false);
			}
		});
		playerLayout.addView(tvName);

		tvDuration = new TextView(getContext());
		tvDuration.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				editStat(false);
			}
		});
		playerLayout.addView(tvDuration);

		return playerLayout;
	}

	private void fillData() {
		if (player != null) {
			tvName.setText(player.getName());
			if (handicap) {
				tvDuration.setVisibility(View.VISIBLE);
			} else {
				tvDuration.setVisibility(View.INVISIBLE);
			}

			if (player.getDuration() / 60 * 60 == player.getDuration()) {
				tvDuration.setText("" + player.getDuration() / 60 + " minutes");
			} else {
				tvDuration.setText("" + player.getDuration() + " secondes");
			}
			tvName.setVisibility(View.VISIBLE);
		} else {
			tvName.setVisibility(View.INVISIBLE);
			tvDuration.setVisibility(View.INVISIBLE);
		}
	}

	private Button createEditButton() {
		bt = new Button(getContext());
		bt.setText("...");
		if (player == null) {
			bt.setBackgroundColor(Color.WHITE);
			bt.setVisibility(INVISIBLE);
		} else {
			bt.setBackgroundColor(player.getColor());
		}
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				editStat(false);
			}
		});
		return bt;
	}

	/**
	 * Set the new play stat. Call this method only if the current play stat is
	 * null.
	 * 
	 * @param rule
	 *            the new rule
	 */
	public void setPlayer(Player player) {
		if (this.player == null) {
			this.player = player;
			img.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.remove));
			fillData();
			bt.setBackgroundColor(player.getColor());
			bt.setVisibility(VISIBLE);
			editStat(true);
		}
	}

	private void editColor(final Button button) {
		ColorPickerDialog.OnColorChangedListener listener = new ColorPickerDialog.OnColorChangedListener() {

			@Override
			public void colorChanged(String key, int color) {
				button.setBackgroundColor(color);
				player.setColor(color);
			}
		};
		ColorPickerDialog dlg = new ColorPickerDialog(getContext(), listener, "KEY", player.getColor(), player.getColor());
		dlg.show();

	}

	public interface OnPlayerClickedListener {
		void playerClicked(PlayerView v);
	}

	private void editStat(final boolean newPlayer) {
		final int oldColor = player.getColor();
		final AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
		final View vvv = LayoutInflater.from(getContext()).inflate(R.layout.edit_player_data, null);
		final EditText txtPlayer = (EditText) vvv.findViewById(R.id.txtPlayer);
		final Button btColor = (Button) vvv.findViewById(R.id.btColor);
		final TextView lbDuration = (TextView) vvv.findViewById(R.id.duration_label);
		final LinearLayout lDuration = (LinearLayout) vvv.findViewById(R.id.duration_layout);
		if (handicap) {
			lbDuration.setVisibility(View.VISIBLE);
			lDuration.setVisibility(View.VISIBLE);
		} else {
			lbDuration.setVisibility(View.GONE);
			lDuration.setVisibility(View.GONE);
		}
		btColor.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				editColor(btColor);
			}
		});

		final EditText txtDuration = (EditText) vvv.findViewById(R.id.txtDuration);
		final Spinner cbDuration = (Spinner) vvv.findViewById(R.id.cbDurationType);
		txtPlayer.setText(player.getName());
		if (player.getDuration() / 60 * 60 == player.getDuration()) {
			txtDuration.setText("" + player.getDuration() / 60);
			cbDuration.setSelection(0);
		} else {
			txtDuration.setText("" + player.getDuration());
			cbDuration.setSelection(1);
		}
		btColor.setBackgroundColor(player.getColor());

		alert.setView(vvv);
		alert.setPositiveButton(getResources().getText(R.string.save), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
				player.setName("" + txtPlayer.getText());
				int duration = 0;
				if (txtDuration.getText().length() > 0) {
					duration = Integer.parseInt("" + txtDuration.getText());
					if (cbDuration.getSelectedItemPosition() == 0) {
						duration *= 60;
					}
				}
				player.setDuration(duration);
				bt.setBackgroundColor(player.getColor());
				fillData();
			}
		});

		alert.setNegativeButton(getResources().getText(R.string.cancel), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
				dialog.cancel();
				player.setColor(oldColor);
				if (newPlayer) {
					playerClickedListener.playerClicked(PlayerView.this);
				}
			}
		});
		alert.show();

	}

	public void setHandicap(boolean handicap) {
		this.handicap = handicap;
		fillData();
	}
}
